<template>
  <el-dialog :model-value="dialogVisible" title="系统提示" width="600px" :before-close="handleClose">
    <div class="userCardInfo">
      <el-image class="userCardInfo__avatar" :src="avatar" :preview-src-list="[avatar]"></el-image>
      <div class="userCardInfo__con">
        <el-form ref="formRef" :model="curRow" label-width="120px">
          <el-form-item label="姓名">
            <el-input placeholder="请输入姓名" v-model="curRow.empname"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="printInfo">
      <div class="printInfo__title">录入指纹（不限定手指）</div>
      <div class="printInfo__con">
        <el-checkbox-group v-model="printCheckList">
          <el-checkbox label="1" disabled>第一次</el-checkbox>
          <el-checkbox label="2" disabled>第二次</el-checkbox>
          <el-checkbox label="3" disabled>第三次</el-checkbox>
          <el-checkbox label="4" disabled>第四次</el-checkbox>
          <el-checkbox label="5" disabled>第五次</el-checkbox>
          <el-checkbox label="6" disabled>第六次</el-checkbox>
        </el-checkbox-group>
      </div>
    </div>
    <el-button class="downloadExe" type="danger" :icon="Download" link @click="_downloadExe">下载服务程序</el-button>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="danger" plain :disabled="printCheckList.length >= 6" :loading="loading" @click="_enterPrint">{{
          curRow.bfinger ? "重新录入" : "开始录入"
        }}</el-button>
        <el-button type="warning" plain :disabled="printCheckList.length < 6" :loading="loading1" @click="_savePrint"
          >保存指纹</el-button
        >
        <el-button type="success" plain :disabled="printCheckList.length < 6" :loading="loading2" @click="_comparePrint"
          >比对指纹</el-button
        >
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="tsx" name="PrintHandleDialog">
import { ref, watch } from "vue";
import { ElMessage } from "element-plus";
import { Download } from "@element-plus/icons-vue";
import { msgMap, printMsgMap, VITE_API_URL } from "@/config";
import { convertImageToBase64, downloadEvt, getAssetsFile } from "@/utils";
import { compareMode, getFinger, getFingerMode } from "@/api/modules/common";
import { compareFinger, saveFinger } from "@/api/modules/cardIssuingCenter/virtualCardManage";

const props = defineProps<{ curRow: { [key: string]: any }; dialogVisible: boolean; handleClose: () => void }>();

watch(
  () => props.dialogVisible,
  () => {
    if (props.curRow.bfinger) {
      fingerID.value = 7;
      printCheckList.value = Array.from({ length: 6 }, (item, index) => "" + (index + 1));
    } else {
      fingerID.value = 1;
      printCheckList.value = [];
    }
    avatar.value = getAssetsFile("images/user.gif");
    convertImageToBase64(`${VITE_API_URL}/employee/photo/download?empid=${props.curRow.empid}`, (base64Value: string) => {
      avatar.value = base64Value;
    });
  }
);

const loading = ref(false);
const loading1 = ref(false);
const loading2 = ref(false);
const avatar = ref("");
const fingerID = ref(1);
const printCheckList = ref([]);
/**
 * 录入指纹
 */
const _enterPrint = async () => {
  if (fingerID.value === 1) printCheckList.value = [];
  if (fingerID.value > 6) {
    return false;
  }
  loading.value = true;
  try {
    const data = (await getFinger(fingerID.value)) as { msgCode: string | number; msgName: string };
    loading.value = false;
    if (data.msgCode == 0) {
      ElMessage.success(`第${fingerID.value}次录入成功`);
      if (fingerID.value <= 6) {
        printCheckList.value.push("" + fingerID.value);
      }
      if (fingerID.value < 6) fingerID.value++;
    } else if (data.msgCode == 2) {
      ElMessage.error(msgMap.get(33));
    } else if (data.msgCode == 3) {
      ElMessage.error(printMsgMap.get(3));
    } else if (data.msgCode == 4) {
      ElMessage.error(printMsgMap.get(4));
    } else if (data.msgCode == 5) {
      ElMessage.error(printMsgMap.get(5));
    } else if (data.msgCode == 6) {
      ElMessage.error(printMsgMap.get(6));
    } else if (data.msgCode == 7) {
      ElMessage.error(printMsgMap.get(7));
    } else if (data.msgCode == 10) {
      ElMessage.error(printMsgMap.get(10));
    } else if (data.msgCode == -2) {
      ElMessage.error(msgMap.get(34));
    } else if (data.msgCode == -3) {
      ElMessage.error(msgMap.get(35));
    } else if (data.msgCode == -4) {
      ElMessage.error(msgMap.get(36));
    } else if (data.msgCode == -100) {
      ElMessage.error(msgMap.get(-1));
    } else {
      ElMessage.error(msgMap.get(36) + "[" + data.msgCode + "]");
    }
  } catch (error) {
    loading.value = false;
    console.log(error);
  }
};

/**
 * 保存指纹
 */
const _savePrint = async () => {
  loading1.value = true;
  try {
    const data = (await getFingerMode()) as { msgCode: string | number; msgName: string };

    loading1.value = false;
    if (data.msgCode == 0) {
      await saveFinger({ id: props.curRow.empid, stringParam1: data.msgName });
      ElMessage.success("保存成功");
    } else if (data.msgCode == 2) {
      ElMessage.error(msgMap.get(33));
    } else if (data.msgCode == 3) {
      ElMessage.error(printMsgMap.get(3));
    } else if (data.msgCode == 4) {
      ElMessage.error(printMsgMap.get(4));
    } else if (data.msgCode == 5) {
      ElMessage.error(printMsgMap.get(5));
    } else if (data.msgCode == 6) {
      ElMessage.error(printMsgMap.get(6));
    } else if (data.msgCode == 7) {
      ElMessage.error(printMsgMap.get(7));
    } else if (data.msgCode == 10) {
      ElMessage.error(printMsgMap.get(10));
    } else if (data.msgCode == -2) {
      ElMessage.error(msgMap.get(23));
    } else if (data.msgCode == -3) {
      ElMessage.error(msgMap.get(38));
    } else if (data.msgCode == -4) {
      ElMessage.error(msgMap.get(37));
    } else if (data.msgCode == -100) {
      ElMessage.error(msgMap.get(-1));
    } else {
      ElMessage.error(msgMap.get(39) + "[" + data.msgCode + "]");
    }
  } catch (error) {
    loading1.value = false;
    console.log(error);
  }
};

/**
 * 比对指纹
 */
const _comparePrint = async () => {
  loading2.value = true;
  try {
    const res = await compareFinger({ id: props.curRow.empid });
    doCompareMode(res.data?.value);
    loading2.value = false;
  } catch (error) {
    loading2.value = false;
    console.log(error);
  }
};

/**
 * 比对指纹
 */
async function doCompareMode(val) {
  try {
    const data = (await compareMode(val)) as { msgCode: string | number; msgName: string };
    if (data.msgCode == 0) {
      ElMessage.success("比对成功");
    } else if (data.msgCode == 2) {
      ElMessage.error(msgMap.get(33));
    } else if (data.msgCode == 3) {
      ElMessage.error(printMsgMap.get(3));
    } else if (data.msgCode == 4) {
      ElMessage.error(printMsgMap.get(4));
    } else if (data.msgCode == 5) {
      ElMessage.error(printMsgMap.get(5));
    } else if (data.msgCode == 6) {
      ElMessage.error(printMsgMap.get(6));
    } else if (data.msgCode == 7) {
      ElMessage.error(printMsgMap.get(7));
    } else if (data.msgCode == 9) {
      ElMessage.error(printMsgMap.get(9));
    } else if (data.msgCode == 10) {
      ElMessage.error(printMsgMap.get(10));
    } else if (data.msgCode == 23) {
      ElMessage.error(printMsgMap.get(23));
    } else if (data.msgCode == -2) {
      ElMessage.error(msgMap.get(23));
    } else if (data.msgCode == -3) {
      ElMessage.error(msgMap.get(41));
    } else if (data.msgCode == -4) {
      ElMessage.error(msgMap.get(40));
    } else if (data.msgCode == -5) {
      ElMessage.error(msgMap.get(42));
    } else if (data.msgCode == -100) {
      ElMessage.error(msgMap.get(-1));
    } else {
      ElMessage.error(msgMap.get(39) + "[" + data.msgCode + "]");
    }
  } catch (error) {
    console.log(error);
  }
}

/**
 * 下载文件
 */
const _downloadExe = () => {
  downloadEvt(getAssetsFile("file/serviceSetup.exe"), "serviceSetup.exe");
};
</script>
<style scoped lang="scss">
.userCardInfo {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  margin-bottom: 10px;
  .userCardInfo__avatar {
    width: 90px;
    height: 90px;
    margin-right: 10px;
    margin-bottom: 18px;
    background: #eeeeee;
    border: 1px solid #eeeeee;
    border-radius: 50%;
  }
  .userCardInfo__con {
    flex: 1;
  }
}
.printInfo {
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: center;
  margin-top: -20px;
  margin-bottom: 20px;
  .printInfo__title {
    width: 100%;
    padding: 10px 0;
    margin-bottom: 20px;
    text-align: center;
    border-bottom: 1px solid #eeeeee;
  }
}
</style>
